<template>
  <!--绑定支付宝-->
  <div id="app" class="my-bindalipay">
      <div class="bindalipay">
          <h3 class="withdraw-title withdraw-title1">设置支付密码</h3>
          <p class="text-p"><span></span>设置个安全的密码</p>
          <div class="input-box" style="margin-top: .50rem;">
              <input  v-model="password1" type="password" placeholder="设置密码(6位数字密码)">
          </div>
          <p class="text-p"><span></span>支付密码很重要,再确认一次</p>
          <div class="input-box input-box1">
              <input  v-model="password2" type="password" placeholder="再次输入新密码">
          </div>
          <div class="bottom-btn" @click="submitpaws">
              <span class="item1"></span>
              <div class="item2">完成</div>
          </div>
      </div>
  </div>
</template>
<script>
    import 'assets/css/common.css'
    import './index.css'
    import common from 'assets/js/common'
export default {
  data() {
    return {
        moneytype:'',//账户类型  type 1,账户，2-钻石，3-魅力值 
        way:'',//操作类型    way=,1-提现，2=转钻石，3-充值  
        money:'',
        password1:'',
        password2:''    
    }
  },
  //实例初始化最之前，无法获取到data里的数据
  beforeCreate(){
    
    
  },  
  //在挂载开始之前被调用
  beforeMount(){
    
  
  },
  //已成功挂载，相当ready()
  mounted(){
       this.password1 = "";
       this.password2 = "";
       this.way = common.getQueryString('way');
       this.moneytype = common.getQueryString('moneytype');
       this.money = common.getQueryString('money');
  },
  //相关操作事件
  methods: {
        //提交设置密码
        submitpaws(){
            var _this = this;
            if(this.password1 == this.password2){
                common.ajax({
                    type:'POST',
                    url:'api/pay/password/set',
                    data:{
                        password:this.password1
                    }
                }).then(res=>{
                    common.errMessage(res.info);
                    this.password1 = "";
                    this.password2 = "";
                    setTimeout(function(){
                        window.location.href = '/views/withdrawdpt/index.html?moneytype='+_this.moneytype+'&way='+_this.way+"&h5=true";
                    },1000);
                    
                })
            }else{
                common.errMessage('两次密码需要一致');
            }
        },
        //提现接口
        getwithdrawApi(){
            var type= '';
            if(this.moneytype == 1){
                type = 'money';
            }

            if(this.moneytype == 3){
                type = 'charm';
            }
            common.ajax({
                type:'POST',
                url:'api/user/withdrawals',
                data:{
                   charm_money_num:this.money,
                   currency:type
                }
            }).then(res=>{
                common.errMessage(res.info);
                //window.location.href = '/views/withdrawdpt/index.html?moneytype='+this.moneytype+'&way='+this.way+"&isH5=true";
            })           
        },
        //到资质审核页
        goCheck:function(){
            if(!this.trim(this.account,'g')){
                this.confirm(`请输入支付宝账号（手机/邮箱）`);
            }else if(!this.trim(this.code,'g')){
                this.confirm(`请输入验证码`);
            }else{
               if((this.hasAlipay.length>0)){
                    this.changealipay();
               }else{
                   this.bindalipay();
               }
            }

        },
        getmobile:function(num){
            if(num){
                var str = num.toString();
                var str1 = str.substring(0,3);
                var str2 = str.substring(7);
                var str3 = str1+'****'+str2;
                return str3
            }
        },
        //倒计时
        timeOut:function(){
           console.log(this.timenum);

            if (this.timenum <= 0) {
                clearInterval(this.timefun);
                this.timefun = '';
                this.timenum = 60;
                this.sendtext = "发送验证码";
                return;                    
            }else{
                this.timenum -= 1;
                this.sendtext = this.timenum+"s后重发";                    
            }
        },
        //发送验证码接口
        sendcode(){

            if(this.sendtext == "发送验证码"){
                this.sendtext = "发送中...";

                var _this = this;
                common.ajax({
                    type:'POST',
                    url:"/api/user/sendcode",
                    data:{
                        mobile:this.mobile,
                        type:this.codetype
                    }
                }).then(response=>{
                    console.log(this.getmobile(this.mobile));
                    var formatMobile = this.getmobile(this.mobile);

                    this.confirm(`验证码已发送您的手机${formatMobile},请注意查收！`);

                    this.timefun = setInterval(()=>{
                        _this.timeOut();
                    }, 1000); 

                }).catch(response=>{
                    this.sendtext = "发送验证码"
                })
            }
        },
        //看是否有支付宝
        getalipay(){
              common.ajax({
                  type:'GET',
                  url:"api/user/withdrawaldata?currency=charm"
              }).then(res=>{
                  
                  this.hasAlipay = res.data.account;
                  //修改支付宝
                  if(res.data.account.length>0){
                      this.paytext = res.data.account;
                      this.codetype = 5;
                  }else{
                      //绑定支付宝
                      this.paytext = '请输入提现账号(手机号/邮箱)';
                      this.codetype = 4;
                  }
              })
        },
        //绑定支付宝
        bindalipay(){
              common.ajax({
                  type:'POST',
                  url:"/api/adder/bindaccount",
                  data:{
                      account:this.account,
                      code:this.code
                  }
              }).then(res=>{
                  this.confirm(res.info);
                  window.location.href = '/views/checkstatus/index.html?h5=true';
              })
        },
        //修改支付宝
        changealipay(){
              common.ajax({
                  type:'POST',
                  url:"/api/adder/updateaccount",
                  data:{
                      account:this.account,
                      code:this.code
                  }
              }).then(res=>{
                  this.confirm(res.info);
                  window.location.href = '/views/checkstatus/index.html?h5=true';
              })
        }, 
        trim:function(str,is_global){
            var result;
            result = str.replace(/(^\s+)|(\s+$)/g,"");
            if(is_global.toLowerCase()=="g"){
                result = result.replace(/\s/g,"");
             }
            return result;
        },
        confirm : function (error) {
            Toast({
                message: error,
                duration: 2500,
                position: 'bottom',
                //iconClass: 'icon icon-success'
            });
        }        
  }
}
</script>

<style>

</style>
